package com.mall.web.controller;



import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.untils.HttpClientUtil;
import com.mall.web.service.TestYCSService;



@Controller
@RequestMapping(value = "/testYCS")
public class TestYCSController {
	private Logger logger=LoggerFactory.getLogger(TestYCSController.class);

	@Resource TestYCSService testYCSService;
	
	
	@RequestMapping("/test")
	@ResponseBody
	public String test(HttpServletRequest request,HttpServletResponse response){
		String data="2";
		Map<String, Object> params=WebUtils.getParametersStartingWith(request, "");
		try {
			data=testYCSService.saveA(params);
		} catch (Exception e) {
			data="3";
		}
		
		return data;
	}
	
	//手机支付宝测试回调
	@RequestMapping("/mobileNotifyUrl")
	@ResponseBody
	public String  mobileNotifyUrl(HttpServletRequest request,HttpServletResponse response){
		String data="2";
		Map<String, Object> params=WebUtils.getParametersStartingWith(request, "");
		try {
			data=testYCSService.saveMo(params);
		} catch (Exception e) {
			data="3";
		}	
		System.out.println("手机支付宝测试回调"+data);
		String urlString="https://www.baidu.com/";
		if("1".equals(data)){						
			urlString="http://www.ide365.com/";
		}

		return urlString;		
	}

	//pc支付宝测试回调
	@RequestMapping("/notifyUrl")
	@ResponseBody
	public String notifyUrl(HttpServletRequest request,HttpServletResponse response) {
		String data="2";
		Map<String, Object> params=WebUtils.getParametersStartingWith(request, "");
		System.out.println(params);
		try {
			data=testYCSService.saveNo(params);
		} catch (Exception e) {
			data="3";
		}	
		System.out.println("pc支付宝测试回调"+data);
		String urlString="https://www.baidu.com/";
		if("1".equals(data)){						
			urlString="http://www.ide365.com/";
		}
		return urlString;
		

	}
	
	
	//测试post方法
	@RequestMapping("/getUrl")
	@ResponseBody
	public void getUrl(){
		String notify_url="http://testbp.ide365.com/testYCS/notifyUrl";	
		List<NameValuePair> param=new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("orderId","10"));
		param.add(new BasicNameValuePair("type","success"));
		param.add(new BasicNameValuePair("orderType","1"));
		/*
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("orderId", out_trade_no);
		map.put("type", "success");		
		JSONObject jsonObject = JSONObject.fromObject(map); 
		
		HttpClientUtil.postJson(notify_url, jsonObject.toString());
		*/
		String body=HttpClientUtil.post(notify_url, param);
		String body1="1";
	}
	
	
	
}
